<template lang="pug">
  .CreateAccountModal
    Modal(
      v-model="isShowCreatePositionModal"
      :title="createPositionModalTitle"
      class-name="vertical-center-modal"
      width="720"
    )
      Form.ivu-row.has-rules-item2.ivu-form-basic(ref="formData" :model="formData" :rules="rules" :label-width="120")
        FormItem.form-item.ivu-col(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop" v-show="item.isShow")
          template(v-if="item.type === 'input'")
            Input(v-model="formData[item.model]" :placeholder="item.placeholder" :disabled="item.isDisabled")
          template(v-if="item.type === 'select'")
            Select(v-model="formData[item.model]" :placeholder="item.placeholder" filterable)
              Option(v-for="(c_item, c_index) of arrListDown[item.options]" :key="c_index" :value="c_item.value") {{c_item.label}}
          template(v-if="item.type === 'datePicker'")
            DatePicker.w-100(:value="formData[item.model]" :placeholder="item.placeholder" type="date")
          //- template(v-if="item.type === 'cascader'")
          //-   .areaTree
          //-     Cascader(v-model="formData[item.model]" :data="areaTree" clearable @on-change="areaTreeChange(item.model, $event)")
          template(v-if="item.type === 'cascader'")
            Input(v-model="formData[item.model]" v-show="false")
            Cascader(v-model="formData[item.arrModel]" :data="parentSource" clearable @on-change="handleTreeChange" :change-on-select="true")
              Input(type="text" icon="ios-arrow-down" :value="selectParentObj.label" v-if="selectParentObj")
          template(v-if="item.type === 'switch'")
            i-switch(v-model="formData[item.model]")
      .clearfix(slot="footer")
        Button(type="text" @click="cancel") 取消
        Button(type="primary" @click="handleSubmit") 保存
</template>

<script>
import { Common } from '@/utils/common.js'
import { PositionApi } from '@/api/Position.api.js'
export default {
  name: 'CreatePositionModal',

  watch: {
  },

  computed: {
  },

  data () {
    return {
      isShowCreatePositionModal: false,
      createPositionModalTitle: '',
      basicDataId: null,
      formData: {
        id: '',
        departmentId: '',
        departmentName: '',
        code: '',
        name: '',
        parentId: '',
        parentIdArr: [],
        effectTime: '',
        enabled: true,
        type: '0'
      },
      formItem: [
        { type: 'input', label: '所属部门：', placeholder: '请输入所属部门', prop: 'departmentName', model: 'departmentName', style: 'ivu-col-span-12', isDisabled: true, isShow: true },
        { type: 'input', label: '职位编码：', placeholder: '请输入职位编码', prop: 'code', model: 'code', style: 'ivu-col-span-12', isDisabled: true, isShow: true },
        { type: 'input', label: '职位名称：', placeholder: '请输入职位名称', prop: 'name', model: 'name', style: 'ivu-col-span-12 is-required', isShow: true },
        { type: 'cascader', label: '上级职位：', placeholder: '请选择上级职位名称', prop: 'parentId', model: 'parentId', arrModel: 'parentIdArr', style: 'ivu-col-span-12', options: 'parentSource', isShow: true },
        { type: 'datePicker', label: '生效日期：', placeholder: '请选择生效日期', prop: 'effectTime', model: 'effectTime', style: 'ivu-col-span-12 is-required', isShow: true },
        { type: 'switch', label: '启用：', placeholder: '请选择是否启用', prop: 'enabled', model: 'enabled', style: 'ivu-col-span-12 is-required', isShow: true },
        { type: 'select', label: '是否主管职位：', placeholder: '请选择是否主管职位', prop: 'type', model: 'type', style: 'ivu-col-span-12 is-required', isShow: true, options: 'defaultOps' }
      ],
      rules: {
      },
      arrListDown: {
        defaultOps: [
          { label: '是', value: '1' },
          { label: '否', value: '0' }
        ]
      },
      parentSource: [],
      areaTree: [], // 中国所有城市树
      selectParentObj: null
    }
  },

  mounted () {
    this.areaTree = JSON.parse(localStorage.getItem('areaTree')) // 获取中国所有城市树
  },

  methods: {
    // 监听所有调用的方法
    listenCall (methodsWords, param) {
      this[methodsWords](param)
    },

    cancel () {
      this.isShowCreatePositionModal = false
    },

    show () {
      this.isShowCreatePositionModal = true
      this.$refs.formData.resetFields()
      this.selectParentObj = null
      console.log('点击是的show()')
      console.log('this.formData: ', this.formData)
    },

    handleTreeChange (value, selectedData) {
      console.log('value: ', value)
      console.log('💗 selectedData: ', selectedData)
      this.selectParentObj = selectedData[selectedData.length - 1]
      this.formData.parentIdArr = value
      this.formData.parentId = selectedData[selectedData.length - 1].value
    },

    // 提交表单
    handleSubmit () {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          console.log('提交表单：', this.formData)
          PositionApi.save(this.formData).then(res => {
            if (res.status === 200 && res.data.success) {
              this.isShowCreatePositionModal = false
              this.$Message.success(res.data.message)
              this.$emit('handleSubmit')
              this.$emit('queryTreeList')
            } else {
              this.$Message.error({
                content: res.data.message,
                duration: 10,
                closable: true
              })
            }
          })
        } else {
          this.$Message.warning('输入有误，请返回检查!')
        }
      })
    },

    // 根据ID获取用户信息
    async getModel (id, parentId) {
      if (!id) {
        this.formItem[1].isShow = false
      } else {
        this.formItem[1].isShow = true
      }
      var params = {
        id: id,
        parentId: parentId
      }
      let res = await PositionApi.getModel(params)
      console.log('获取modal res: ', res)
      if (res.status === 200 && res.data.success) {
      // if (res.status === 200) {
        var data = res.data.data
        this.parentSource = data.parentSource
        Object.keys(this.formData).forEach(key => {
          if (key == 'type') {
            this.formData[key] = data[key] + ''
          } else {
            this.formData[key] = data[key] ? data[key] : ''
          }
        })
        this.formData.parentId = data.parentId ? data.parentId : ''
        this.formData.parentIdArr = data.fullParentId ? data.fullParentId.split(',') : []
        this.selectParentObj = {
          label: data.parentName,
          id: data.parentId
        }
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 所在地区选择
    areaTreeChange (model, value) {
      this.formData[model] = value
      console.log('model: ', model)
      console.log('value: ', value)
    }
  }
}
</script>

<style lang="less" scoped>

</style>
